<?php 
	namespace Admin\Controller;
	use Think\Controller;

	class GoodsController extends CommonController{
		public function index(){
			//查询数据
			$model=D('goods');

			//做模糊查询传值
			$name=$_GET['name'];
			
			//总条数
			$total=$model->where("name like'%$name%'")->count();
			//总页数
			$page=new \Think\Page($total,3);
			$page->setConfig('header','总页数');
			$page->setConfig('prev','上一页');
			$page->setConfig('next','下一页');
			//获取当前页
			$p=$page->nowPage;
			//echo $p;
			//exit;
			$page_str=$page->show();
			$limit=$page->firstRow.','.$page->listRows;

			$list=$model->getAll($name,$limit);
			//var_dump($list);exit;
			foreach ($list as &$value) {
				$value['giname']='/Public/Uploads/'.$this->sub($value['giname']).'80_'.$value['giname'];
			}
			//发送数据
			$this->assign('page',$page_str);
			$this->assign('p',$p);

			$this->assign('list',$list);
			//加载模板
			$this->display();
		}

		public function delete(){
			$id=$_GET['id'];
			//查询商品信息以便得到图片
			$model=D('goods');
			$list=$model->getAll();
			//var_dump($list);exit;
			$goods=M('goods');
			//开启事务
			$goods->startTrans();

			$res=$goods->where('id='.$id)->delete();

			if ($res) 
			{//删除图片
				$goods_img=M('goods_img');
				$result=$goods_img->where('goods_id='.$id)->delete();				
			}
			if ($result) 
			{	//删除路径下的图片
				foreach ($list as $value) 
				{				
					$this->setUnlink($value['giname']);
				}

				$goods->commit();
				$this->success('删除成功',U('Goods/index'),2);
			}else{
				$goods->rollback();
				$this->error('删除失败');
			}
		}

	public function add()
	{
		if (empty($_POST)) {
			//展示出页面分类下拉列表
			$cateModel=M('category');

			$sql="select `id`,`name`,`pid`,`path`,concat(path,id) bpath from `category` order by bpath";
			$list=$cateModel->query($sql);
			//var_dump($list);exit;
			foreach ($list as $key => &$value) 
			{
				//第几级
				$num=count(explode(',', rtrim($value['path'],',')))-1;
				$list[$key]['name']=str_repeat('&nbsp;&nbsp;', $num).$value['name'];

				//var_dump($list);
			}
			$this->assign('list',$list);
			$this->display();
			return false;		
	}
			$model=D('goods');
			//根据表单提交的POST数据创建数据对象
			$data=$model->create();
			if (!$data) {
			// 如果创建失败表示验证没有通过 输出错误提示信息
				$this->error($model->getError());
			}else{
				$filename=$this->upload();
				//$data['cate_id']=1;
				$data['user_id']=$_SESSION['admin']['id'];
				$res=$model->setAdd($data,$filename);
				if ($res) {
					//添加成功
					$this->success('添加成功',U('Goods/index'),2);
					return false;
				}else{
					$this->setUnlink($filename);
					$this->error('添加失败');
				}
			}

		}

		//文件上传
		public function upload(){
			//实例化上传类
			$upload=new \Think\Upload();
			//设置附件上传大小
			$upload->maxSize=3145728;
			//设置附件上传类型
			$upload->exts=array('jpg','gif','png','jpeg');
			//设置附件上传目录
			$upload->rootPath='./Public/';
			$upload->savePath='./Uploads/';
			$upload->saveName=date('Ymd',time()).uniqid();
			$upload->autoSub=true;
			$upload->subName=date('Y',time()).'/'.date('m',time()).'/'.date('d',time());
			//上传文件,返回成功上传的文件信息数组
			$info=$upload->upload();
			/*var_dump($info);
			exit;
			array (size=1)
		  'pic' => 
		    array (size=9)
		      'name' => string '5.jpg' (length=5)
		      'type' => string 'image/jpeg' (length=10)
		      'size' => int 64052
		      'key' => string 'pic' (length=3)
		      'ext' => string 'jpg' (length=3)
		      'md5' => string '308914d44a526c4610ed219909e785a4' (length=32)
		      'sha1' => string 'df2e3ab92965cc7e15914d1338fac9edb08abbf8' (length=40)
		      'savename' => string '201607125784f8328d613.jpg' (length=25)
		      'savepath' => string './Uploads/2016/07/12/' (length=21)*/
			if (!$info) 
			{
				//上传错误提示错误信息
				$this->error($upload->getError());
			}else{
				//上传成功
				//做图片压缩
				$this->zoom($info['pic']['savename']);
				return $info['pic']['savename'];
			}
		}

		private function sub($str)
		{
			return substr($str, 0,4).'/'.substr($str, 4,2).'/'.substr($str, 6,2).'/';
		}

		private function zoom($filename)
		{
			$image=new \Think\Image();
			$path='./Public/Uploads/'.$this->sub($filename);
			$image->open($path.$filename);
			//按照原图的比例生成一个缩略图并保存
			$image->thumb(233,322)->save($path.'322_'.$filename);
			$image->thumb(150,150)->save($path.'150_'.$filename);
			$image->thumb(300,300)->save($path.'300_'.$filename);
			$image->thumb(98,125)->save($path.'125_'.$filename);
			$image->thumb(80,80)->save($path.'80_'.$filename);
		}

		private function setUnlink($filename){
			$path='./Public/Uploads/';
			unlink($path.$this->sub($filename).$filename);
			unlink($path.$this->sub($filename).'150_'.$filename);
			unlink($path.$this->sub($filename).'322_'.$filename);
			unlink($path.$this->sub($filename).'300_'.$filename);
			unlink($path.$this->sub($filename).'125_'.$filename);
			unlink($path.$this->sub($filename).'80_'.$filename);
		}

	public function bast()
		{
			//接受当前页
			$p=$_GET['p'];
		//var_dump($p);exit;
			$where['id']=$_GET['id'];

			$model=M('goods');
			$status=$model->where($where)->select();
			//var_dump($status);
			$data['is_bast']=$status[0]['is_bast']==0?1:0;
			//var_dump($data);
			$res=$model->where($where)->save($data);
			//var_dump($res);exit;
			if ($res) 
			{
				$this->redirect('Goods/index',['p'=>$p]);
			}
		}
	public function hot()
		{
			//接受当前页
			$p=$_GET['p'];
		//var_dump($p);exit;
			$where['id']=$_GET['id'];

			$model=M('goods');
			$status=$model->where($where)->select();
			//var_dump($status);exit;
			$data['is_hot']=$status[0]['is_hot']==0?1:0;
			//var_dump($data);
			$res=$model->where($where)->save($data);
			//var_dump($res);exit;
			if ($res) 
			{
				$this->redirect('Goods/index',['p'=>$p]);
			}
		}

		public function nnew()
		{
			//接受当前页
			$p=$_GET['p'];
		//var_dump($p);exit;
			$where['id']=$_GET['id'];

			$model=M('goods');
			$status=$model->where($where)->select();
			//var_dump($status);exit;
			$data['is_new']=$status[0]['is_new']==0?1:0;
			//var_dump($data);
			$res=$model->where($where)->save($data);
			//var_dump($res);exit;
			if ($res) 
			{
				$this->redirect('Goods/index',['p'=>$p]);
			}
		}

	public function detail()
	{
		//接受当前页
		$p=$_GET['p'];
		$id=$_GET['id'];
	//连表查询

	$model=D('goods');
	$list=$model->getDetail($id);
	foreach ($list as $k => $v) 
	{
		$list[$k]['giname']='/Public/Uploads/'.$this->sub($v['giname']).'150_'.$v['giname'];
		$list[$k]['gtime']=date('Y年m月d日',$v['gtime']);
	}

	$list=$list[0];
	//var_dump($list);
	$imgModel=M('goods_img');
	$sql="select `name` from `goods_img` where goods_id=$id and is_cover=0";
	$imglist=$imgModel->query($sql);
	//var_dump($imglist);
	foreach ($imglist as $ke => $val) 
	{
		$imglist[$ke]['name']='/Public/Uploads/'.$this->sub($val['name']).'125_'.$val['name'];
	}
	//var_dump($imglist);exit;
		$this->assign('list',$list);
		$this->assign('imglist',$imglist);
		$this->assign('p',$p);
		$this->display();
	}

	public function addimg()
	{
		if (empty($_POST)) {
			
			$id=$_GET['id'];
			$this->assign('id',$id);
			$this->display();
			return false;
		}

		$imgModel1=M('goods_img');
		//商品的id
		$id=$_POST['id'];
		//var_dump($id);exit;
		//上传图片
		$filename=$this->upload();

		//接收数据库
		$data['name']=$filename;
		$data['is_cover']=$_POST['is_cover'];
		$data['goods_id']=$id;
		//var_dump($data);exit;
		//插入之前判断是否封面
		if ($data['is_cover']) 
		{
			//更改封面及添加封面,那么原封面变成子封面(修改)
			$dat['is_cover']=0;
			$where['goods_id']=$id;
			$result=$imgModel1->where($where)->save($dat);
			//添加新的封面
			$res=$imgModel1->add($data);
			if ($res) 
			{
				$this->success('添加图片成功',U('Goods/detail',['id'=>$id]),1);
			}
			}else{
				//不添加封面，添加封面下的图片
				$res=$imgModel1->add($data);
				//var_dump($res);exit;
				if ($res) 
				{
					$this->success('添加图片成功',U('Goods/detail',['id'=>$id]),1);
				}
			}
	}
}
?>